package com.example.app2024.threadsafe;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.app2024.R;

public class threadActivity extends AppCompatActivity
{


      TextView textView;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_thread);

            textView = findViewById(R.id.textView10);
      }

      public void dothread(View view)
      {
            int id = view.getId();
            if (id == R.id.btn_thread01)
            {
                  new Thread(new Runnable()
                  {

                        @Override
                        public void run()
                        {
                              runOnUiThread(new Runnable()
                              {
                                    @Override
                                    public void run()
                                    {
                                          textView.setText("bbbbb");
                                    }
                              });

                        }
                  }).start();
            }
            else if (id == R.id.btn_thread02)
            {
                  textView.post(new Runnable()
                  {
                        @Override
                        public void run()
                        {
                              textView.setText("cccccc");
                        }
                  });
            }
            else if (id == R.id.btn_thread03)
            {
                  textView.postDelayed(
                          new Runnable()
                          {
                                @Override
                                public void run()
                                {
                                      textView.setText("eeeeee");
                                }
                          }, 2000);
            }
            else if (id == R.id.btn_thread04)
            {
                  new Handler(Looper.getMainLooper()).post(new Runnable()
                  {
                        @Override
                        public void run()
                        {
                              textView.setText("fffffffff");
                        }
                  });
            }
            else if (id == R.id.btn_thread05)
            {
                  new TestTask().execute(0);
            }
      }

      class TestTask extends AsyncTask<Integer, Integer, String>
      {
            @Override
            protected void onPreExecute()
            {
                  super.onPreExecute(); //主线程, 在doinbackground 之前
                  textView.setText("加载中, 等一秒.....");
            }

            @Override
            protected String doInBackground(Integer... integers)
            {
                  try
                  {
                        Thread.sleep(1000);
                        return "zzzzzz";
                  }
                  catch (InterruptedException e)
                  {
                        throw new RuntimeException(e);
                  }
            }

            @Override
            protected void onPostExecute(String s)
            {
                  super.onPostExecute(s);
                  textView.setText(s);
            }
      }

}